<?php
session_start();

define('HOME_DIR', dirname(__FILE__).'/../');
define('DATA_DIR', HOME_DIR.'data/');
define('LIB_DIR', HOME_DIR.'lib/');
define('INC_DIR', HOME_DIR.'inc/');
define('POLLS_DIR', DATA_DIR.'sondages/');

include(LIB_DIR.'lib.php');
include(LIB_DIR.'utilisateurs.php');
include(LIB_DIR.'sondages.php');

include(ds('configuration'));
include(ds('utilisateurs'));
include(ds('sondages'));
include(ds('sondage'));

$Configuration = new Configuration();
$Utilisateurs = new Utilisateurs();
$Sondages = new Sondages();

if(!isset($need_admin)) $need_admin = false;
if(!isset($need_connect)) $need_connect = false;
if(!isset($need_install)) $need_install = false;

// http://snipplr.com/view/7149/remove-magic-quotes/
if (get_magic_quotes_gpc()) {
	function _strip_quotes(&$var) {
		if (is_array($var)) {
			array_walk($var, '_strip_quotes');
		} else {
			$var = stripslashes($var);
		}
	}
 
	foreach (array('GET','POST','COOKIE') as $v) {
		if (!empty(${"_".$v})) { array_walk(${"_".$v}, '_strip_quotes'); }
	}
}

if($need_admin) {
	$style_path = '../';
}

if(!file_exists(DATA_DIR.'configuration.ds') && $need_install) {
	redirect($style_path.'install.php');
}
elseif(file_exists(DATA_DIR.'configuration.ds') && !$need_install) {
	redirect('index.php');
}
if(file_exists(DATA_DIR.'configuration.ds') && $need_install) {
	$Configuration->load(DATA_DIR.'configuration.ds');
	$Utilisateurs->load(DATA_DIR.'utilisateurs.ds');
	$Sondages->load(DATA_DIR.'sondages.ds');
}

if($need_connect && !connecte()) {
	redirect($style_path.'index.php');
}
if($need_admin && !admin()) {
	redirect('../');
}


ob_start();
?> 
